package com.example.week02.day1.Activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;


import com.example.week02.R;


//模拟QQ登录页面
public class QQ_layout extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.qq_layout);

        RedAndBlue();
    }

    public void RedAndBlue(){
        TextView tvAgreement = findViewById(R.id.QQ_TV);
        String fullText = "登录即代表阅读并同意服务条款";
        SpannableString spannable = new SpannableString(fullText);

        int serviceStart = fullText.indexOf("服务条款");
        int colorRed = ContextCompat.getColor(this, R.color.red);
        int colorBlue = ContextCompat.getColor(this, R.color.blue);

        // 1. 设置前半部分红色（无点击事件）
        spannable.setSpan(
                new ForegroundColorSpan(colorRed),
                0,
                serviceStart,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        );

        // 2. 创建点击Span（蓝色+可点击）
        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                // 点击事件逻辑（示例：跳转到服务条款页面）
                startActivity(new Intent(QQ_layout.this, caculator_layout.class));
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                ds.setColor(colorBlue); // 设置文本颜色为蓝色
                ds.setUnderlineText(false); // 移除下划线
            }
        };

        // 3. 应用点击Span到特定范围
        spannable.setSpan(
                clickableSpan,
                serviceStart,
                serviceStart + 4,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        );

        tvAgreement.setText(spannable);

        // 4. 关键配置（必须添加以下两行）
        tvAgreement.setMovementMethod(LinkMovementMethod.getInstance()); // 启用点击
        tvAgreement.setHighlightColor(Color.TRANSPARENT); // 移除点击背景色
    }
}